<?php

class CollectionPropertyTable extends Doctrine_Table {

    public static function getInstance() {
        return Doctrine_Core::getTable('CollectionProperty');
    }

    /**
     * Method to get CollectionProperties with an appropriate orderby statement
     * ensures properties are grouped together in display and edit forms
     */
    public function getCollectionPropertiesQuery(CollectionItem $collectionItem, $orderByUinOnly = false) {
        $q = $this->createQuery();
        $q->innerJoin('CollectionProperty.PropertyDetail pd');
        $q->where('collectionUin = ?', $collectionItem->getUin());
        if ($orderByUinOnly) {
            $q->orderBy('CollectionProperty.uin');
        } else {
            $q->orderBy('pd.displayGroupBy, pd.typeName, pd.typeValue, CollectionProperty.uin');
        }
        return $q;
    }

     public function getCollectionPropertiesByIdentifierQuery(CollectionItem $collectionItem, $identifier, $orderByUinOnly = false) {
        $q = $this->getCollectionPropertiesQuery($collectionItem, $orderByUinOnly);
        $q->andWhere('pd.typeValue = ?',$identifier);
        return $q;
    }

}